//
//  BXliveSetVC.m
//  BXlive
//
//  Created by bxlive on 2019/7/29.
//  Copyright © 2019 cat. All rights reserved.
//

#import "BXVideoSetVC.h"
#import "HHSwitchSettingCell.h"
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <Masonry/Masonry.h>


@interface BXVideoSetVC () <UITableViewDataSource, UITableViewDelegate>

@property(nonatomic,strong)UIView *navigationView;
@property (nonatomic,strong) UITableView *mainTableView;//表单视图
@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation BXVideoSetVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationItem.title = NSLocalizedString(@"短视频功能设置", nil);
    self.fd_prefersNavigationBarHidden = YES;
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    [self setupNavigationView];
    _dataArray = @[NSLocalizedString(@"WIFI下自动连播", nil),NSLocalizedString(@"允许视频被下载", nil)];
    [self createTableView];
}
-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"短视频功能设置", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
}

- (void)createTableView
{
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.backgroundColor = [UIColor clearColor];
    self.mainTableView.showsVerticalScrollIndicator = YES;
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.mainTableView];
    WS(weakSelf);
   [_mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.right.bottom.mas_equalTo(0);
       make.top.equalTo(weakSelf.navigationView.mas_bottom);
   }];
}

-(void)switchAction:(BOOL)on indexRow:(NSInteger)row {
    NSString *value;
    if (on) {
        value = @"1";
    }else {
        value = @"0";
    }
    NSString *name;
    if (row){
        name = @"download_switch";
    } else {
        name = @"autoplay_switch";
    }
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:row];
    [[NewHttpRequestHuang sharedNewHttpRequestHuang] UserSwitchStatusWithName:name Value:value Success:^(id responseObject) {
        if ([responseObject[@"code"]intValue]==0) {
            BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
            if (row){
                liveUser.download_switch = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"download_switch"]];
                
            } else {
                liveUser.autoplay_switch = [NSString stringWithFormat:@"%@",responseObject[@"data"][@"autoplay_switch"]];
            }
            [BXLiveUser setCurrentBXLiveUser:liveUser];
        } else {
            [self.mainTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [BGProgressHUD showInfoWithMessage:responseObject[@"msg"]];
        }
    } Failure:^(NSError *error) {
        [self.mainTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"设置失败", nil)];
    }];
}

#pragma mark - UITableView delegate/dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HHSwitchSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[HHSwitchSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    WS(ws);
    cell.switchFunction = ^(BOOL on) {
        [ws switchAction:on indexRow:indexPath.row];
    };
    BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
    if(indexPath.row){
        cell.functionSwitch.selected = [liveUser.download_switch isEqualToString:@"1"];
    } else {
        cell.functionSwitch.selected = [liveUser.autoplay_switch isEqualToString:@"1"];
    }
    cell.titleLabel.text = _dataArray[indexPath.row];
    return cell;
}

@end
